﻿package XFComponent
{	
	public class XFNumberEffect extends XFLabel
	{		
		/**
		 *  
		 */		
		private var preciousValue : uint	=	0;
		
		/**
		 * 
		 * @param val
		 * 
		 */		
		public function set precious( val : uint ) : void
		{
			preciousValue	=	val;
		}
		
		/**
		 * 
		 */		
		private var currentValue : Number	=	0;
		
		/**
		 * 
		 * @param val
		 * 
		 */		
		public function set value( val : Number ) : void
		{
			currentValue	=	val;
			increaseValue	=	val;
			
			if( preciousValue == 0 )
				this.text		=	int(currentValue).toString();
				
			else
				this.text		=	currentValue.toFixed( preciousValue ) ;
		}
		
		/**
		 * 
		 * @param val
		 * 
		 */		
		public function get value() : Number
		{
			return currentValue;
		}
		
		/**
		 * 
		 */		
		private var increaseValue : Number	=	0;
		
		/**
		 * 
		 * @param val
		 * 
		 */		
		public function set increase( val : Number ) : void
		{
			increaseValue	=	val;
			delta			=	Math.abs( currentValue - increaseValue );
			sign			=	increaseValue >= currentValue ? 1.0 : -1.0;
			if( delta > 30.0 )
			{				
				if( sign > 0.0 )
					currentValue	=	increaseValue - 30.0;
				else
					currentValue	=	increaseValue + 30.0;
			}			
		}
		
		/**
		 * 
		 * @param val
		 * 
		 */		
		public function get increase() : Number
		{
			return increaseValue;
		}
		
		/**
		 * 
		 * @param val
		 * 
		 */	
		private var delta : Number	=	0.0;
		
		/**
		 * 
		 * @param val
		 * 
		 */	
		private var sign : Number	=	1.0;
		
		/**
		 * 
		 * @param val
		 * 
		 */	
		public function onFrameUpdate( deltaTime : Number, totalTime : Number ) : void
		{
			if( delta != 0 )
			{				
				// Show number on screen
				currentValue	+=	deltaTime * sign * 10.0;				
				
				// Stop animation if delta less than epsilon value
				if( ( currentValue - increaseValue ) * sign > 0 )
				{
					currentValue	=	increaseValue;
					delta	=	0.0;
				}
				
				if( preciousValue == 0 )
					this.text		=	int(currentValue).toString();
					
				else
					this.text		=	currentValue.toFixed( preciousValue ) ;
			}
		}
		
		public static function moneyFormat( money : String ):String
		{
			var str : String = String( money );
			var numDot :int =  str.length / 3;
			var indexBegin : int =  str.length % 3;
			var result:String = "";
			var temp:String = str.substr(0, indexBegin);
			for( var i : int = 0; i<= numDot; i++)
			{
				result += temp;
				if(i != numDot && temp.length != 0)
					result += ".";
				temp	= 	str.substr(indexBegin, 3);
				indexBegin += 3;
			}
			return result;
		}
	}
}
